<template>
  <div class="component-upload-image">
    <el-form :inline="true">
      <el-upload
        action
        accept=".jpg,.jpeg,.png"
        list-type="picture-card"
        :show-file-list="false"
        style="display: inline-block; vertical-align: top"
        :disabled="true">
        <el-image v-if="imageModel.materialUrl == ''" :src="imageModel.materialUrl">
          <div slot="error" class="image-slot" @click="handleSelectImage">
            <i class="el-icon-plus" />
          </div>
        </el-image>
        <div v-else class="image">
          <el-image
            :src="imageModel.materialUrl"
            :style="`width:146px;height:146px;`"
            fit="contain"
          />
          <div class="mask">
            <div class="actions">
              <span title="预览" @click.stop="dialogVisible = true" style="margin:0 2px">
                <i class="el-icon-zoom-in" />
              </span>
              <span title="移除" @click.stop="handleRemoveImage" style="margin:0 2px">
                <i class="el-icon-delete" />
              </span>
            </div>
          </div>
        </div>
      </el-upload>
    </el-form>
    
    <el-dialog
      :visible.sync="dialogVisible"
      title="预览"
      width="800"
      append-to-body
    >
      <img
        :src="imageModel.materialUrl"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>
    <el-dialog
      v-if="materialDialogVisible"
      :visible.sync="materialDialogVisible"
      title="素材库"
      width="1400px"
      append-to-body
      top="5%">
      <div class="dialogForm">
        <Material hasDialog hasSingle @selectMaterialData="handleSelectMaterialData"></Material>
      </div>
    </el-dialog>
    
  </div>
</template>

<script>
import store from "@/store";
import Material from '@/views/material/material/index.vue';


export default {
  model:{
    prop:'image',
    event:'commitSingleMaterial'
  },
  data() {
    return {
      dialogVisible: false,
      // 资料库选择框
      materialDialogVisible:false,
      imageModel:this.image,
      previewItem:null,
      isFirst: true,
      returnValue:[]
    };
  },
  props: {
    // image:{
    //   type: String,
    //   default:() => []
    // }
    image:{
      type: Object,
      default:() => {}
    }
  },
  watch:{
    image:{
      handler(newValue){
        this.imageModel = newValue ? newValue : {materialId:'', materialUrl:''}
      },
      immediate: true,
      deep:true
    }
  },
  mounted(){
    //this.init()
  },
  components:{
    Material
  },
  destroyed(){
    this.$bus.$off("selectMaterialData")
  },
  methods: {

    /**
     * 修改model
     */
    modifyModel(){
      this.$emit("commitSingleMaterial", this.imageModel)
    },

    /**
     * 移除图片
     */
    handleRemoveImage() {
      this.imageModel = null
      this.modifyModel()
    },

    /**
     * 选择图片
     */
    handleSelectImage(){
      this.materialDialogVisible = true
    },

    /**
     * 预览
     */
    handlePreview(item){
      this.previewItem = item
      this.dialogVisible = true
    },

    //重置表单
    closeDialog() {
      this.materialDialogVisible = false
    },

    /**
     * 选择素材
     */
    handleSelectMaterialData(items){
      this.imageModel = {
        materialId: items[0].id,
        materialUrl: items[0].materialUrl
      }
      console.log(this.imageModel)
      this.modifyModel()
      this.closeDialog()
    }
  }
};
</script>

<style scoped lang="scss">
.image {
  position: relative;

  .mask {
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    height: 146px;
    background-color: rgba(0, 0, 0, 0.5);
    transition: all 0.3s;
  }

  &:hover .mask {
    opacity: 1;
  }
}

::v-deep .el-dialog__body {
  padding: 0;
}
::v-deep .dialogForm .form-outer {
  padding: 0 20px;
  .form-inner {
    padding: 0;
  }
}
</style>